package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.Dao;
import com.mohe.dangjian.model.CommunityProject;
import com.mohe.dangjian.model.CommunityProjectActivity;
import com.mohe.dangjian.service.interfaces.ICommunityProjectActivity;
import com.mohe.dangjian.utils.Page;

public class CommunityProjectActivityImpl implements ICommunityProjectActivity {
	
	private Dao<CommunityProjectActivity> communityProjectActivityDao;

	public Dao<CommunityProjectActivity> getCommunityProjectActivityDao() {
		return communityProjectActivityDao;
	}

	public void setCommunityProjectActivityDao(
			Dao<CommunityProjectActivity> communityProjectActivityDao) {
		this.communityProjectActivityDao = communityProjectActivityDao;
	}

	@Override
	public List<CommunityProjectActivity> getCommunityProjectActivityList(
			CommunityProjectActivity communityProjectActivity, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProjectActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(communityProjectActivity.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProjectActivity.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProjectActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProjectActivity.getContent() + "%%");
		}
		// 情况
		if (!StringUtils.isBlank(communityProjectActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + communityProjectActivity.getSituation() + "%%");
		}
		// 所属项目
		CommunityProject cprj = communityProjectActivity.getCommunityProject();
		if (cprj != null) {
			where.append(" and communityProject.timeKey = ?");
			params.add(cprj.getTimeKey());
		}

		where.append(" order by createDate desc");
		List<CommunityProjectActivity> list = communityProjectActivityDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(CommunityProjectActivity communityProjectActivity) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProjectActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(communityProjectActivity.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProjectActivity.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProjectActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProjectActivity.getContent() + "%%");
		}
		// 情况
		if (!StringUtils.isBlank(communityProjectActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + communityProjectActivity.getSituation() + "%%");
		}
		// 所属项目
		CommunityProject cprj = communityProjectActivity.getCommunityProject();
		if (cprj != null) {
			where.append(" and communityProject.timeKey = ?");
			params.add(cprj.getTimeKey());
		}		

		return communityProjectActivityDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveCommunityProjectActivity(
			CommunityProjectActivity communityProjectActivity) {
		return communityProjectActivityDao.insert(communityProjectActivity);
	}

	@Override
	public boolean saveCommunityProjectActivityList(
			List<CommunityProjectActivity> communityProjectActivityList) {
		return communityProjectActivityDao.insert(communityProjectActivityList);
	}

	@Override
	public CommunityProjectActivity getCommunityProjectActivityById(Long timeKey) {
		return communityProjectActivityDao.getByKey(timeKey);
	}

	@Override
	public boolean updateCommunityProjectActivity(
			CommunityProjectActivity communityProjectActivity) {
		return communityProjectActivityDao.update(communityProjectActivity);
	}

	@Override
	public boolean delCommunityProjectActivity(Long timeKey) {
		return communityProjectActivityDao.delete(timeKey);
	}

	@Override
	public List<CommunityProjectActivity> getAllCommunityProjectActivity() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return communityProjectActivityDao.query(where.toString(), null);
	}

	@Override
	public List<CommunityProjectActivity> getCommunityProjectActivityList(
			CommunityProjectActivity communityProjectActivity) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProjectActivity where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(communityProjectActivity.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProjectActivity.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProjectActivity.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProjectActivity.getContent() + "%%");
		}
		// 情况
		if (!StringUtils.isBlank(communityProjectActivity.getSituation())) {
			where.append(" and situation like ?");
			params.add("%%" + communityProjectActivity.getSituation() + "%%");
		}
		// 所属项目
		CommunityProject cprj = communityProjectActivity.getCommunityProject();
		if (cprj != null) {
			where.append(" and communityProject.timeKey = ?");
			params.add(cprj.getTimeKey());
		}

		where.append(" order by createDate desc");
		List<CommunityProjectActivity> list = communityProjectActivityDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

}
